package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.ledger.LedgerProduceGatherDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.utils.ExcelUtil;
import com.zjuee.core.utils.ExportExcel;
import com.zjuee.base.model.common.ledger.LedgerProduceGather;
import com.zjuee.service.LedgerProduceGatherService;
import com.zjuee.vo.LedgerProduceGatherVo;
import com.zjuee.web.ledger.mapper.LedgerProduceGatherMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerProduceGatherServiceImpl implements LedgerProduceGatherService {

    @Resource
    private LedgerProduceGatherMapper ledgerProduceGatherMapper;
    @Resource
    LoginDto loginDto;
    @Resource
    TranslateData translateData;

    @Override
    public Long add(LedgerProduceGather type) {
        String year = String.valueOf(DateUtils.getYear(new Date()));
        QueryWrapper<LedgerProduceGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bci_id", type.getBciId())
                .eq("wlpg_scode", type.getWlpgScode())
                .eq("wlpg_name", type.getWlpgName())
                .eq("wldg_year", year);
        LedgerProduceGather ledgerProduceGather = ledgerProduceGatherMapper.selectOne(queryWrapper);
        if (ledgerProduceGather != null) {
            throw new ServiceException("本年度该台账已存在，不能重复添加!");
        }
        type.setWlpgYear(year);
        type.setWlpgCreator(loginDto.getUserName());
        type.setWlpgCreateTime(new Date());
        return Long.parseLong(String.valueOf(ledgerProduceGatherMapper.insert(type)));
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        LedgerProduceGather ledgerProduceGather = ledgerProduceGatherMapper.selectById(id);
        if (!ledgerProduceGather.getBciId().equals(validateId)) {
            throw new ServiceException("无权限完成操作!");
        }
        int deleteById = ledgerProduceGatherMapper.deleteById(id);
        if (deleteById <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(LedgerProduceGather type) {
        LedgerProduceGather ledgerProduceGather = ledgerProduceGatherMapper.selectById(type.getId());
        if (ledgerProduceGather == null) {
            throw new ServiceException("台账信息不能为空!");
        }
        int updateById = ledgerProduceGatherMapper.updateById(type);
        if (updateById <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public LedgerProduceGather selectById(Long id, Long validateId) {
        LedgerProduceGather ledgerProduceGather = ledgerProduceGatherMapper.selectById(id);
        if (!ledgerProduceGather.getBciId().equals(validateId)) {
            throw new ServiceException("无权限完成操ZsLedgerBillServiceListHandle作!");
        }
        return ledgerProduceGather;
    }

    @Override
    public Page<LedgerProduceGather> list(LedgerProduceGatherDto dto) {
        dto.setBciId(loginDto.getOrgBusinessId());
        Page<LedgerProduceGather> ledgerProduceGatherPage = ledgerProduceGatherMapper.selectPageList(dto.getPage().convert(), dto);
        translateData.translateData(ledgerProduceGatherPage.getRecords(),
                "LedgerProduceGatherListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
                String.valueOf(DictionaryType.WL_EDIT_STATUS.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())

        );
        return ledgerProduceGatherPage;
    }

    @Override
    public LambdaQueryWrapper<LedgerProduceGather> fuzzyQuery(LedgerProduceGatherDto dto) {
        return null;
    }


    @Override
    public void exportExcel(LedgerProduceGather ledgerProduceGather) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        List<LedgerProduceGather> ledgerProduceGathers = ledgerProduceGatherMapper.selectList(ledgerProduceGather);
        if (!CollectionUtils.isEmpty(ledgerProduceGathers)) {
            //翻译数据
            List<Map<String, Object>> dataMap = (List<Map<String, Object>>) translateData.translateData(ledgerProduceGathers,
                    "LedgerProduceGatherListHandle",
                    String.valueOf(DictionaryType.WL_FROM.getValue()),
                    String.valueOf(DictionaryType.WL_EDIT_STATUS.getValue()),
                    String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())
            );
            String[] key = {"id", "wlpgModelStr", "wlpgTypeStr", "wlpgBcode", "wlpgScode", "wlpgName", "wlpgYear", "wlpgTotalProduce", "wlpgPreStock", "wlpgTotalTrustDispose", "wlpgTotalDispose", "wlpgStock", "wlpgChange", "wlpgCreateTime", "wlpgModifiedTime"};
            String[] str = {"序号", "废物大类别", "废物小类别", "废物大类", "废物小类", "废物名称", "年份", "产生量总计(吨)", "上年底库存量(吨)", "委托处置量总计(吨)", "自行处置量总计(吨)", "当前库存量(吨)", "库存变动量(吨)", "创建时间", "修改时间"};

            List<ExportExcel> exportExcelList = new ArrayList<ExportExcel>();
            ExportExcel e1 = new ExportExcel("产生台账列表", "产生台账列表", key, str, dataMap);
            exportExcelList.add(e1);
            ExcelUtil.download("产生台账列表", exportExcelList, request, response);
        }

    }

    @Override
    public List<LedgerProduceGather> getList(LedgerProduceGather ledgerProduceGather) {
        ledgerProduceGather.setBciId(loginDto.getOrgBusinessId());
        List<LedgerProduceGather> ledgerProduceGathers = ledgerProduceGatherMapper.selectList(ledgerProduceGather);
        //翻译数据
        translateData.translateData(ledgerProduceGathers,
                "LedgerProduceGatherListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
                String.valueOf(DictionaryType.WL_EDIT_STATUS.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())
        );
        return ledgerProduceGathers;
    }


    @Override
    public List<LedgerProduceGather> getListMap(LedgerProduceGatherVo condition) {
        return ledgerProduceGatherMapper.getListMap(condition);
    }

    @Override
    public void updateBatch(ArrayList<LedgerProduceGather> updateLedger) {
        ledgerProduceGatherMapper.updateBatch(updateLedger);
    }

    @Override
    public LedgerProduceGather findOneByCondition(LedgerProduceGather ledgerProduceGather) {
        return ledgerProduceGatherMapper.findOneByCondition(ledgerProduceGather);
    }
}
